/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.me.androidhelloapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

/**
 *
 * @author rlutz
 */
public class TTSSMSReceiver  extends BroadcastReceiver {

    public static final String SMSRECEIVED = "SMSR";
    private static final String SMS_REC_ACTION =
      "android.provider.Telephony.SMS_RECEIVED";

    @Override
//    public void onReceive(Context context, Intent intent) {
//        if (intent. getAction( ) .
//             equals(TTSSMSReceiver.SMS_REC_ACTION) ) {
//            StringBuilder sb = new StringBuilder() ;
//            Bundle bundle = intent. getExtras() ;
//            if ( bundle != null) {
//                Object[] pdus = (Object[]) bundle.get("pdus");
//                for (Object pdu : pdus) {
//                    SmsMessage smsMessage =
//                         SmsMessage.createFromPdu((byte[])pdu) ;
//                    sb. append( "body - "
//                         + smsMessage. getDisplayMessageBody() ) ;
//                }
//            }
//            Log.v("TTSSMSReceiver", "SMS RECEIVED - " + sb.toString());
//        }
//    }

    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Object[] pdusObj = (Object[]) bundle.get("pdus");
        SmsMessage[] messages = new SmsMessage[pdusObj.length];
        for (int i = 0; i < pdusObj.length; i++) {

            messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
            String msgTxt = messages[i].getMessageBody();
            Log.v("TTSSMSReceiver", "SMS RECEIVED - " + msgTxt.toString());
           // if (msgTxt.equals("whoa")) {
                Intent startActivity = new Intent();
                startActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity.setAction("com.apress.START_THE_MUSIC");
                context.startActivity(startActivity);
           // }
        }
        
        Toast toast = Toast.makeText(context, "Received SMS: " + messages[0].getMessageBody(), Toast.LENGTH_LONG);
        toast.show();
    }

}